package com.lfz.search.controller;

import com.lfz.common.vo.SysResult;
import com.lfz.search.service.SearchService;
import com.lfz.search.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/search")
public class SearchController {

	@Autowired
	private SearchService searchService;

	// url数组传参
	// reservePrice%5b%5d=555&reservePrice%5b%5d=777
	// reservePrice[]=555&reservePrice[]=777
	// reservePrice = [555, 777]
	@RequestMapping("")
	public SysResult search(@RequestParam(required = false, defaultValue = "") String q,
							@RequestParam(required = false, defaultValue = "job_item") String searchType,
							@RequestParam(required = false, defaultValue = "0") Integer begin,
							@RequestParam(required = false, defaultValue = "40") Integer rows,
							@RequestParam(required = false, defaultValue = "", value = "loc[]") List<Integer> loc,
							@RequestParam(required = false, defaultValue = "", value = "sort[]") List<String> sort,
							@RequestParam(required = false, defaultValue = "", value = "cat[]") List<Integer> cat,
							@RequestParam(required = false, defaultValue = "", value = "tag[]") List<Integer> tag,
							@RequestParam(required = false, defaultValue = "", value = "reservePrice[]") List<Integer> reservePrice) {

		Page page = new Page();
		page.setQ(q);
		page.setSearchType(searchType);
		page.setBegin(begin);
		page.setRows(rows);
		page.setLoc(loc);
		page.setSort(sort);
		page.setCat(cat);
		page.setTag(tag);
		page.setReservePrice(reservePrice);
		System.out.println(page);
		try {
			page = searchService.search(page);
			return SysResult.oK(page);
		} catch (Exception e) {
			e.printStackTrace();
			return SysResult.build(201, e.getMessage());
		}
		
	}

}
